package com.vegas.netspider.apps.parser.sport.football;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.vegas.netspider.apps.parser.WebParser;
import com.vegas.netspider.dto.MatchScore;
import com.vegas.netspider.dto.SportMatchUtil;

public class ScoreParser implements WebParser<List<MatchScore>> {

	private Pattern ftScorePattern=Pattern.compile("<tr\\s*?class=\"b_cen\">\\s*?<td>(.*?)<br>(.*?)</td>\\s*?<td>(.*?)<br>(.*?)</td>\\s*?<td align=\"left\">(.*?)<br>(.*?)</td>\\s*?<td><font color=\"#CC0000\"><b><span style=\"overflow:hidden;\">(.*?)</span><br><span style=\"overflow:hidden;\">(.*?)</span></b></font></td>\\s*?<td><font color=\"#CC0000\"><b><span style=\"overflow:hidden;\">(.*?)</span><br><span style=\"overflow:hidden;\">(.*?)</span></b></font></td>\\s*?<td>(.*?)</td>\\s*?</tr>");
	@Override
	public List<MatchScore> parse(String res) {
		if(res==null||res.equals("")){
			return null;
		}
		Matcher matcher = ftScorePattern.matcher(res);
		
		List<MatchScore> list = new ArrayList<MatchScore>();
		while(matcher.find()){
			String date = matcher.group(1);
			String time = matcher.group(2);
			
			long playTime = SportMatchUtil.parsePlayTime(date+" "+time);
			
			String htmid = matcher.group(3);
			Integer htMatchId = Integer.valueOf(htmid);
			String gtmid = matcher.group(4);
			Integer gtMatchId = Integer.valueOf(gtmid);
			
			String htName = matcher.group(5);
			String gtName = matcher.group(6);
			
			String htHalfScore = matcher.group(7);
			String gtHalfScore = matcher.group(8);
			String htFullScore = matcher.group(9);
			String gtFullScore = matcher.group(10);
			
			
			MatchScore ms = new MatchScore();
			ms.setPlayTime(playTime);
			ms.setHomeTeamName(htName);
			ms.setHomeTeamId(htMatchId);
			ms.setGuestTeamName(gtName);
			ms.setGuestTeamId(gtMatchId);
			
			ms.setHtFirstHalfScore(htHalfScore);
			ms.setHtFullSocre(htFullScore);
			ms.setGtFirstHalfScore(gtHalfScore);
			ms.setGtFullSocre(gtFullScore);
			
			String existsMore = matcher.group(11);
			if(existsMore.indexOf("MORE")>-1){
				ms.setExistsMore(true);
			}
			list.add(ms);
			
		}
		return list;
	}

}
